﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Othello_Game;

namespace OneOnOneRunner
{
    class Play
    {

        public WeightVector Player1Weights, Player2Weights;

        public MinMaxTreeSearch MMTS = new MinMaxTreeSearch();

        public int PlayAGame(WeightVector P1W, WeightVector P2W)
        {
            this.Player1Weights = P1W;
            this.Player2Weights = P2W;

            Board board = new Board(8, 8);

            for (int i = 0; i < 60; i++)
            {
                int turn = (i % 2) + 1;
                if (turn == 1) GeneralConsts.Instance.AttributeWeights = Player1Weights;
                else GeneralConsts.Instance.AttributeWeights = Player2Weights;
                ActionList toDo = MMTS.FindBest(board, 3);
                if (toDo != null)
                {
                    board.Play(toDo);
                }
                board.SwitchPlayers();
            }
            int p1count = 0;
            int p2count = 0;
            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    if (board.GetPlayer(new Position(row, col)) == players.PLAYER1) p1count++;
                    else if (board.GetPlayer(new Position(row, col)) == players.PLAYER2) p2count++;
                }
            }
            if (p1count > p2count) return 1;
            else if (p2count > p1count) return 2;
            return 0;
        }

        public void playGames(int howManyGames)
        {
            WeightVector P1W = new WeightVector(new double[] { 0.2, 0.2, 0.2, 0.2, 0.2 });
            WeightVector P2W = new WeightVector(new double[] { 0.1, 0.3, 0.1, 0.3, 0.2 });
            Play play = new Play();
            int p1wins = 0;
            int p2wins = 0;
            int result = 0;

            for (int i = 0; i < howManyGames; i++)
            {
                result = play.PlayAGame(P1W, P2W);
                if (result == 1) p1wins++;
                else if (result == 2) p2wins++;
            }
        }

    }
}
